//
//  ContactPersonCell.m
//  HuaRun
//
//  Created by admin on 17/8/28.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "ContactPersonCell.h"
#import "LineView.h"
#import "Contact_ListModel.h"
#import "ContactPersonModel.h"

@interface ContactPersonCell ()
@property (nonatomic, strong) UIButton * firstCharBtn;
//名字
@property (nonatomic, strong) UILabel * nameLab;
//中文名字
@property (nonatomic, strong) UILabel * nameLabChi;
//公司
@property (nonatomic, strong) UILabel * companyLab;

//分割线
@property (nonatomic, strong) LineView * lineView;

@end

@implementation ContactPersonCell

#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self viewInit];
        [self layoutInit];
//        [self testMethod];
        [self methodBind];
    }
    return self;
}

- (void)methodBind {
    weakself(ws);
    [self.phoneBtn addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        ws.callPhoneBlock ? ws.callPhoneBlock([ws getHighLeverTelephoneNum]) : nil;
    }];

    [self.emailBtn addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        ws.sendEmailBlock ? ws.sendEmailBlock([ws getHighLeverEmailStr]) : nil;
    }];

}

- (void)viewInit {
    [super viewInit];
    
    [self.contentView addSubview:self.firstCharBtn];
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.nameLabChi];

    [self.contentView addSubview:self.companyLab];
    [self.contentView addSubview:self.phoneBtn];
    [self.contentView addSubview:self.emailBtn];
    
    [self.contentView addSubview:self.lineView];
}

- (void)layoutInit {
    
    weakself(ws);
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.contentView).offset(kMargin10);
        make.right.mas_equalTo(ws.contentView).offset(-kMargin10);
        make.top.mas_equalTo(ws.contentView).offset(0);
        make.bottom.mas_equalTo(ws.contentView).offset(0);
    }];

    [self.firstCharBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView).offset(kMargin10);
        make.top.mas_equalTo(ws.nameLab).offset(kMargin10);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];

    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.firstCharBtn.mas_right).offset(kMargin10);
        make.top.mas_equalTo(ws.backView).offset(kMargin10);
        make.right.mas_equalTo(ws.phoneBtn.mas_left).offset(-kMargin10);
        make.height.mas_equalTo(20);
    }];

    [self.nameLabChi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(ws.nameLab);
        make.top.mas_equalTo(ws.nameLab.mas_bottom);
    }];

    [self.companyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.nameLab);
        make.top.mas_equalTo(ws.nameLabChi.mas_bottom).offset(kMargin10);
        make.right.mas_equalTo(ws.backView).offset(-kMargin10);
    }];

    [self.phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.nameLab);
        make.right.mas_equalTo(ws.emailBtn.mas_left).offset(-2 * kMargin10);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    
    [self.emailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.nameLab);
        make.right.mas_equalTo(ws.backView.mas_right).offset(-kMargin10);
        make.size.mas_equalTo(ws.phoneBtn);
    }];

    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.backView);
        make.left.mas_equalTo(ws.backView).offset(kMargin10);
        make.right.mas_equalTo(ws.emailBtn).offset(0);
        make.height.mas_equalTo(1);
    }];

}


//- (void)testMethod {
//    [_firstCharBtn setTitle:@"A" forState:(UIControlStateNormal)];
////    _nameLab.text = [NSString stringWithFormat:@"Admin %@", kTestStr];
//    [_nameLab resetLineSpaceHeight5WithStr:kTestStr];
//    _companyLab.text = kTestStr;
//    
//}

-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

#pragma mark - getter setter
- (void)setContactPersonModel:(ContactPersonModel *)contactPersonModel {
    _contactPersonModel = contactPersonModel;

    //默认 是没有显示间隔的， 重新设置约束
    weakself(ws);
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.contentView).offset(kMargin10);
        make.right.mas_equalTo(ws.contentView).offset(-kMargin10);
        make.top.mas_equalTo(ws.contentView).offset(kMargin10);
        make.bottom.mas_equalTo(ws.contentView).offset(0);
    }];

    
    //    中文名是：nameChinese英文名是  lastName + firstName
    //     title department accountName
    _nameLab.text =  [NSString stringWithFormat:@"%@ %@",
                      [NSString showSpaceStringWithStr: contactPersonModel.firstName],
                      [NSString showSpaceStringWithStr: contactPersonModel.lastName]];
    _nameLabChi.text = [NSString showSpaceStringWithStr: contactPersonModel.nameChinese ];
    _companyLab.text = [NSString showSpaceStringWithStr:_personModel.accountName];
    
    if (contactPersonModel.firstName.length > 1) {
        NSString * firstStr = [[contactPersonModel.firstName substringToIndex:1]
                               uppercaseString];
        [_firstCharBtn setTitle:[NSString showSpaceStringWithStr:firstStr]
                       forState:(UIControlStateNormal)];
    }else if (contactPersonModel.lastName.length  > 1) {
        NSString * firstStr = [[contactPersonModel.lastName substringToIndex:1]
                               uppercaseString];
        [_firstCharBtn setTitle:[NSString showSpaceStringWithStr:firstStr]
                       forState:(UIControlStateNormal)];
    }
}

- (void)setPersonModel:(Contact_ListModel *)personModel {
    _personModel = personModel;
    
    [_nameLab resetLineSpaceHeight5WithStr:[NSString stringWithFormat:@"%@  %@",
                                            [NSString showSpaceStringWithStr:
                                            _personModel.firstName],
                                            [NSString showSpaceStringWithStr:
                                             _personModel.lastName]]];
    _nameLabChi.text = [NSString showSpaceStringWithStr: _personModel.nameChinese ];

    _companyLab.text = [NSString showSpaceStringWithStr:_personModel.accountName];
    if (_personModel.lastName.length > 1) {
        NSString * firstStr = [[_personModel.lastName substringToIndex:1]
                               uppercaseString];
        [_firstCharBtn setTitle:[NSString showSpaceStringWithStr:firstStr]
                       forState:(UIControlStateNormal)];
        
    }else if (_personModel.firstName.length  > 1) {
        NSString * firstStr = [[_personModel.firstName substringToIndex:1]
                               uppercaseString];
        [_firstCharBtn setTitle:[NSString showSpaceStringWithStr:firstStr]
                       forState:(UIControlStateNormal)];
    }

}

- (NSString *)getHighLeverEmailStr {
    if (![NSString isemptyStr:_personModel.email] && _personModel.email.length > 0) {
        return _personModel.email;
    }
    return @"";
}

//后台会返回四个字段，direct、hkMobile、chinaMobile和general；按照这个顺序判断取打哪个电话
- (NSString *)getHighLeverTelephoneNum {

    if (![NSString isemptyStr:_personModel.direct] && _personModel.direct.length > 0) {
        return _personModel.direct;
    }
    if (![NSString isemptyStr:_personModel.hkMobile] && _personModel.hkMobile.length > 0) {
        return _personModel.hkMobile;
    }
    if (![NSString isemptyStr:_personModel.chinaMobile] && _personModel.chinaMobile.length > 0) {
        return _personModel.chinaMobile;
    }
    if (![NSString isemptyStr:_personModel.general] && _personModel.general.length > 0) {
        return _personModel.general;
    }
    
    return @"";
}

- (UIButton *)firstCharBtn {
    if (!_firstCharBtn) {
        self.firstCharBtn = [UIButton new];
        _firstCharBtn.titleLabel.font = kFontSize10;
        _firstCharBtn.titleLabel.textColor = [UIColor whiteColor];
        
        int n = [self getRandomNumber:1 to:8];
        NSString * str = [NSString stringWithFormat:@"0%d", n];
        [_firstCharBtn setBackgroundImage:[UIImage imageNamed:str]
                                 forState:(UIControlStateNormal)];
    }
    return _firstCharBtn;
}


- (UILabel *)nameLab {
    if (!_nameLab) {
        self.nameLab = [UILabel new];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.font = kGeneralFontSize;
        _nameLab.textColor = [UIColor blackColor];
        _nameLab.numberOfLines = 1;
    }
    return _nameLab;
}

- (UILabel *)nameLabChi {
    if (!_nameLabChi) {
        self.nameLabChi = [UILabel new];
        _nameLabChi.textAlignment = NSTextAlignmentLeft;
        _nameLabChi.font = kGeneralFontSize;
        _nameLabChi.textColor = [UIColor colorWithHex:@"3c3c3c"];
        _nameLabChi.numberOfLines = 1;
    }
    return _nameLabChi;
}

- (UILabel *)companyLab {
    if (!_companyLab) {
        self.companyLab = [UILabel new];
        _companyLab.textAlignment = NSTextAlignmentLeft;
        _companyLab.font = kGeneralFontSize;
        _companyLab.textColor = [UIColor grayColor];
    }
    return _companyLab;
}

- (UIButton *)phoneBtn {
    if (!_phoneBtn) {
        self.phoneBtn = [UIButton new];
        [_phoneBtn setImage:[UIImage imageNamed:@"icon_phone"]
                   forState:(UIControlStateNormal)];
//        [_phoneBtn setBackgroundColor:[UIColor redColor]];
        _phoneBtn.hidden = YES;
    }
    return _phoneBtn;
}

- (UIButton *)emailBtn {
    if (!_emailBtn) {
        self.emailBtn = [UIButton new];
        [_emailBtn setImage:[UIImage imageNamed:@"icon_email"]
                             forState:(UIControlStateNormal)];
//        [_emailBtn setBackgroundColor:[UIColor redColor]];
        _emailBtn.hidden = YES;
    }
    return _emailBtn;
}

- (LineView *)lineView {
    if (!_lineView) {
        self.lineView =  [LineView new];
        
    }
    return _lineView;
}

@end
